﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel.Composition;

using NuMetaheuristics;
using NuMetaheuristics.Genotypes;

namespace NuMetaheuristics.Operators.Vector
{
    /// <summary>
    /// Produces a vector which is the sum between two given vectors.
    /// </summary>
    [Export(typeof(IBinaryOperator<DoubleVectorGenotype>))]
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    public class DoubleVectorAdder : IBinaryOperator<DoubleVectorGenotype>
    {
        public DoubleVectorGenotype Operate(DoubleVectorGenotype operandL, DoubleVectorGenotype operandR)
        {
            if (operandL == null || operandR == null)
                throw new ArgumentNullException();

            if (operandL.Vector.Length != operandR.Vector.Length)
                throw new ArgumentOutOfRangeException("operandR", "Length not equal to operandL");

            DoubleVectorGenotype resultant = new DoubleVectorGenotype(operandL);

            for (int i = 0; i < operandL.Vector.Length; i++)
            {
                resultant.Vector[i] = operandL.Vector[i] + operandR.Vector[i];
            }

            return resultant;
        }
    }

    /// <summary>
    /// Produces a vector which is the sum between two given vectors.
    /// </summary>
    [Export(typeof(IBinaryOperator<IntegerVectorGenotype>))]
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    public class IntegerVectorAdder : IBinaryOperator<IntegerVectorGenotype>
    {
        public IntegerVectorGenotype Operate(IntegerVectorGenotype operandL, IntegerVectorGenotype operandR)
        {
            if (operandL == null || operandR == null)
                throw new ArgumentNullException();

            if (operandL.Vector.Length != operandR.Vector.Length)
                throw new ArgumentOutOfRangeException("operandR", "Length not equal to operandL");

            IntegerVectorGenotype resultant = new IntegerVectorGenotype(operandL);

            for (int i = 0; i < operandL.Vector.Length; i++)
            {
                resultant.Vector[i] = operandL.Vector[i] + operandR.Vector[i];
            }

            return resultant;
        }
    }
}
